package su.msk.jet.team01.chatik.server.commands;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.data.DataManager;
import su.msk.jet.team01.chatik.server.data.Room;
import su.msk.jet.team01.chatik.server.data.RoomManager;
import su.msk.jet.team01.chatik.server.exception.InvalidArgumentException;
import su.msk.jet.team01.chatik.server.exception.InvalidRoomNameException;

public class ChangeRoomCommand extends Command {
	private static final String CLIENT_INFO_MSG_INVALID_ROOM_NAME_LENGTH = "Room name is too long. Maximum room name length is %d.";
	private static final String CLIENT_INFO_MSG_ROOM_CHANGED = "Room successfully changed to %s.";
	private static final int MAX_ROOM_NAME_LENGTH = 100;

	public ChangeRoomCommand() {
		super("room", 1);
	}

	@Override
	public void doExecute(final Client client, final String[] args)
			throws InvalidArgumentException {
		RoomManager roomManager = DataManager.getRoomManager();
		String name = args[0];
		Room room;

		validateHaveNoSpaces(name);
		try {
			validateLength(name, MAX_ROOM_NAME_LENGTH);
			room = roomManager.getRoom(name);
			client.changeRoom(room);
			client.send(CLIENT_INFO_MSG_ROOM_CHANGED, name);
		} catch (InvalidArgumentException e) {
			client.send(CLIENT_INFO_MSG_INVALID_ROOM_NAME_LENGTH,
					MAX_ROOM_NAME_LENGTH);
		} catch (InvalidRoomNameException e) {
			client.send(e.getMessage());
		}
	}
}
